package com.jevons.geo_lib.geopackage;

import android.app.Activity;
import android.content.Context;

import java.io.File;

import mil.nga.geopackage.GeoPackage;
import mil.nga.geopackage.GeoPackageFactory;
import mil.nga.geopackage.GeoPackageManager;

/**
 * Copyright 2020 JxGIS
 *
 * @file CustomGeoPackageManager
 * @auth linaisheng
 * Created on 2021/5/17.
 * Description：
 */
public class CustomGeoPackageManager {

    private static GeoPackageManager mGeoManager;
    private Context mContext;
    private static CustomGeoPackageManager myGeoPackageManager = null;

    private CustomGeoPackageManager(Activity context) {
        this.mContext = context;

        mGeoManager = GeoPackageFactory.getManager(mContext);
    }


    public static CustomGeoPackageManager getInstance(Activity context) {
        if (myGeoPackageManager == null && context != null)
            synchronized (CustomGeoPackageManager.class) {
                if (myGeoPackageManager == null)
                    myGeoPackageManager = new CustomGeoPackageManager(context);
            }

        return myGeoPackageManager;
    }

    public GeoPackageManager getGeoPackageManager() {
        if (mGeoManager == null) {
            GeoPackageFactory.getManager(mContext);
        }
        return mGeoManager;
    }


    public GeoPackage openGeoPackage(String filePath) {
        File file = new File(filePath);

        if (!file.exists()) {
            return null;
        }
        //有数据库，直接打开
        GeoPackage mGeoPackage = mGeoManager.openExternal(file);

        return mGeoPackage;
    }
}
